from google.appengine.ext import db
from Common.Client import Client
from Common.DuplicatedInstanceError import DuplicatedInstanceError


class Campaign(db.Model):
    CampaignName = db.StringProperty(required=True)
    Ref_Client = db.ReferenceProperty(Client, collection_name='Campaigns')
    #Ref_Group = db.ReferenceProperty(Group, collection_name='Campaigns')
    
    def put (self):
        # Make sure e-mails are unique for each user
        if (not self.is_saved()) and Campaign.get_by_key_name(self.CampaignName, self._parent_key):
            raise DuplicatedInstanceError ('Campaign.CampaignName', self.CampaignName)
 
        # call the parent method
        db.Model.put (self)
        
    def TransactionPut(self):
        db.run_in_transaction(self.put)